package com.clps.rulesregulations.service.impl;

import com.clps.rulesregulations.dao.RoleResourceDao;
import com.clps.rulesregulations.service.RoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色资源处理逻辑实现类
 * @author 张超
 * @date 2017/7/11
 */
@Service("roleResourceService")
public class RoleResourceServiceImpl implements RoleResourceService {
    @Autowired
    private RoleResourceDao roleResourceDao;
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @CacheEvict(cacheNames="resources", allEntries=true)
    public void updateRoleResource(Integer roleId, List<Integer> resourceIds) {
        //roleResourceDao
        roleResourceDao.delRoleResourceByRoleId(roleId);

        if(resourceIds!=null && !resourceIds.isEmpty()){
            //将需要添加的资源添加到该角色上
            Map<String,Object> map = new HashMap<>();
            map.put("roleId",roleId);
            map.put("resourceIds",resourceIds);
            roleResourceDao.addRoleResources(map);
        }

    }

    @Override
    public List<Integer> findResourceIdsByRoleId(Integer roleId) {
        return roleResourceDao.findResourceIdsByRoleId(roleId);
    }
}
